<?php
namespace app\home\controller;

use app\home\model\NavModel;
use app\home\model\PicCommentModel;
use app\home\model\PicImgModel;
use app\home\model\PicLikeModel;
use app\home\model\PicModel;
use app\home\model\ProfessionCommentModel;
use app\home\model\ProfessionImgModel;
use app\home\model\ProfessionLikeModel;
use app\home\model\ProfessionModel;
use app\home\model\ProgramCommentModel;
use app\home\model\ProgramLikeModel;
use app\home\model\ProgramModel;
use app\home\model\VideoCommentModel;
use app\home\model\VideoLikeModel;
use app\home\model\VideoModel;
use app\home\model\AdModel;
use app\home\model\EngineeringModel;
use think\Loader;

class Index extends Base
{
    const PROGRAM = 'program';
    const DESIGN  = 'design';
    const INSTALL = 'install';
    const MATERIAL = 'material';
    const PROFESSION = 'profession';
    public function index()
    {
        $ad = new AdModel();
        $ad_list = $ad->get_ad_list();
        $this->assign('ad_list',$ad_list);

        $engineering = new EngineeringModel();
        $temp_list = $engineering->alias('e')->join('sf_user u','e.uid = u.id','left')
            ->field('e.id,e.update_time,u.mobile,e.title')->where(['e.status'=>1])->order('e.update_time desc')->limit(20)->select();
        $engineering_left = [];
        $engineering_right = [];
        foreach ($temp_list as $key=>&$value){
            $value['mobile'] =  substr_replace($value['mobile'], '****', 3, 4);
            if($key % 2== 0){
                $engineering_left[] = $value;
            }else{
                $engineering_right[] = $value;
            }
        }
        $this->assign('e_left',$engineering_left);
        $this->assign('e_right',$engineering_right);
        $module = [];
        $module[self::DESIGN] = ['module_name'=>get_module_name(self::DESIGN),'list'=>$this->get_list(self::DESIGN)];
        $module[self::INSTALL] = ['module_name'=>get_module_name(self::INSTALL),'list'=>$this->get_list(self::INSTALL)];
        $module[self::MATERIAL] = ['module_name'=>get_module_name(self::MATERIAL),'list'=>$this->get_list(self::MATERIAL)];
        $this->assign('module',$module);
        $this->assign('profession',$this->get_profession());
        $this->assign('program',$this->get_program());
        $pic = $this->get_pic();
        $this->assign('pic_first',array_shift($pic));
        $this->assign('pic',$pic);

        $video = $this->get_video();
        $this->assign('video_first',array_shift($video));
        $this->assign('video',$video);
        $model = new EngineeringModel();
        $where = [];
        $where['update_time'] = ['gt',strtotime('today')];
        $where['status'] = ['eq',1];
        $today = $model->where($where)->count();
        $this->assign('today',$today);
        return view();
    }
    private function get_video(){
        $model = new VideoModel();
        $comment_model = new VideoCommentModel();
        $like = new VideoLikeModel();
        $list = $model->where(['status'=>1])->order('update_time')->limit(7)->select();
        foreach ($list as &$value){
            $value['img'] = $value['video'].'?vframe/jpg/offset/0';
            $value['comment_num'] = $comment_model->where(['message_id'=>$value['id']])->count();
            $value['like_num'] = $like->where(['message_id' => $value['id']])->count();
//            if($this->_user_id){
//                $result = $like->where(['uid'=>$this->_user_id,'message_id'=>$value['id']])->find();
//                if($result){
//                    $value['is_like'] = 1;
//                }else{
//                    $value['is_like'] = 0;
//                }
//            }else{
//                $value['is_like'] = 0;
//            }
        }
        return $list;
    }
    private function get_pic(){
        $model = new PicModel();
        $img_model = new PicImgModel();
        $comment_model = new PicCommentModel();
        $like = new PicLikeModel();
        $list = $model->where(['status'=>1])->order('update_time desc')->limit(7)->select();
        foreach ($list as &$value){
            $value['img'] = $img_model->where(['message_id'=>$value['id']])->value('path');
            $value['comment_num'] = $comment_model->where(['message_id'=>$value['id']])->count();
            $value['like_num'] = $like->where(['message_id' => $value['id']])->count();
//            if($this->_user_id){
//                $result = $like->where(['uid'=>$this->_user_id,'message_id'=>$value['id']])->find();
//                if($result){
//                    $value['is_like'] = 1;
//                }else{
//                    $value['is_like'] = 0;
//                }
//            }else{
//                $value['is_like'] = 0;
//            }
        }
        return $list;
    }
    private function get_program(){
        $model = new ProgramModel();
        $comment_model = new ProgramCommentModel();
        $like = new ProgramLikeModel();
        $list = $model->where(['status'=>1])->order('update_time desc')->limit(4)->select();
        foreach ($list as &$value) {
            $value['img'] = '';
            $value['comment_num'] = $comment_model->where(['message_id' => $value['id']])->count();
            $value['like_num'] = $like->where(['message_id' => $value['id']])->count();
//            if($this->_user_id){
//                $result = $like->where(['uid'=>$this->_user_id,'message_id'=>$value['id']])->find();
//                if($result){
//                    $value['is_like'] = 1;
//                }else{
//                    $value['is_like'] = 0;
//                }
//            }else{
//                $value['is_like'] = 0;
//            }
        }
        return $list;
    }
    private function get_profession(){
        $model = new ProfessionModel();
        $img_model = new ProfessionImgModel();
        $comment_model = new ProfessionCommentModel();
        $like = new ProfessionLikeModel();
        $list = $model->where(['status'=>1])->order('update_time desc')->limit(5)->select();
        foreach ($list as &$value) {
            $value['img'] = $img_model->where(['message_id' => $value['id']])->value('path');
            $value['comment_num'] = $comment_model->where(['message_id' => $value['id']])->count();
            $value['like_num'] = $like->where(['message_id' => $value['id']])->count();
//            if($this->_user_id){
//                $result = $like->where(['uid'=>$this->_user_id,'message_id'=>$value['id']])->find();
//                if($result){
//                    $value['is_like'] = 1;
//                }else{
//                    $value['is_like'] = 0;
//                }
//            }else{
//                $value['is_like'] = 0;
//            }
        }
        return $list;
    }
    private function get_list($model_name){
        $model = Loader::model($model_name,'model',true);
        $img_model = Loader::model($model_name.'Img','model',true);
        $comment_model = Loader::model($model_name.'Comment','model',true);
        $like = Loader::model($model_name.'Like','model',true);
        $list = $model->where(['status'=>1])->order('update_time desc')->limit(8)->select();
        foreach ($list as &$value){
            $value['img'] = $img_model->where(['message_id'=>$value['id']])->value('path');
            $value['comment_num'] = $comment_model->where(['message_id'=>$value['id']])->count();
            $value['like_num'] = $like->where(['message_id' => $value['id']])->count();
//            if($this->_user_id){
//                $result = $like->where(['uid'=>$this->_user_id,'message_id'=>$value['id']])->find();
//                if($result){
//                    $value['is_like'] = 1;
//                }else{
//                    $value['is_like'] = 0;
//                }
//            }else{
//                $value['is_like'] = 0;
//            }
        }
        return $list;
    }
}
